import { asyncRouterMap, constantRoutes } from '@/router';

function hasPermission(role, route) {
  if (route.meta && route.meta.role) {
    return  route.meta.role.indexOf(role) >= 0
  } else {
    return true
  }
}

  const state= {
      routers: constantRoutes,
      addRouters: [],
      loadRouters: false
  }
  const mutations= {
      SET_ROUTERS: (state, routers) => {
          state.addRouters = routers
          state.routers = constantRoutes.concat(routers)
      },
      SET_LOAD: (state, status) => {
          state.loadRouters = status
      }
  }
  const actions= {
    GenerateRoutes({ commit }, role) {
      return new Promise(resolve => {
           const  accessedRouters = asyncRouterMap.filter(v => {
            if (hasPermission(role, v)) {
                if (v.children && v.children.length > 0) {
                v.children = v.children.filter(child => {
                    if (hasPermission(role, child)) {
                    return child
                    }
                    return false;
                });
                return v
                } else {
                return v
                }
            }
            return false;
            });
        commit('SET_ROUTERS', accessedRouters);
        commit('SET_LOAD', true)
        resolve();
      })
    },
      ClareRouters({
          commit
      }) {
          return new Promise(resolve => {
              commit('SET_LOAD', false)
              resolve()
          })
      }
  }

  export default {
    namespaced: true,
    state,
    mutations,
    actions
  }
  